/**
 * <p>Original Author:  jessefreeman</p>
 * <p>Class File: ApplyStyleBehavior.as</p>
 *
 * <p>Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:</p>
 *
 * <p>The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.</p>
 *
 * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.</p>
 *
 * <p>Licensed under The MIT License</p>
 * <p>Redistributions of files must retain the above copyright notice.</p>
 *
 * <p>Revisions<br/>
 *		1.0  Initial version Oct 20, 2009</p>
 *
 */
 
package com.flashartofwar.fcss.behavior {
	import com.flashartofwar.fcss.managers.StyleSheetManager;
	import com.flashartofwar.fcss.styles.IStyle;
	import com.flashartofwar.fcss.stylesheets.IStyleSheetCollection;
	import com.flashartofwar.fcss.utils.StyleApplierUtil;

	import flash.events.EventDispatcher;
	import flash.utils.getQualifiedClassName;

	/**
	 * @author jessefreeman
	 */
	public class ApplyStyleBehavior extends EventDispatcher
	{

		protected static const ID_DELIMITER:String = " ";

		protected static const NOTHING_TO_OBSERVE_ERROR:String = "No observable component was set to listen to.";

		protected static const OBSERVABLE_ERROR:String = "Target can not be observed";

		protected var _active:Boolean;

		protected var _className:String;

		protected var _id:String;

		protected var observableComponent:Object;

		protected var defaultSelectorNames:Array;

		protected var styleID:String;

		protected var styleSheetCollection:IStyleSheetCollection = StyleSheetManager.instance;

		/**
		 *
		 * @param styleID
		 * @param target
		 */
		public function ApplyStyleBehavior(styleID:String, target:Object)
		{
			this.styleID = styleID;
			this.observableComponent = target;
			parseStyleNames(styleID);
			applyDefaultStyle();
		}

		/**
		 *
		 */
		public function applyDefaultStyle():void
		{
			var style:IStyle = styleSheetCollection.getSelector.apply(null, defaultSelectorNames);
			applyStyle(style);
		}

		/**
		 *
		 * @param style
		 */
		public function applyStyle(style:IStyle):void
		{
			StyleApplierUtil.applyProperties(observableComponent, style);
		}

		/**
		 *
		 * @return
		 */
		public function get className():String
		{

			if (!_className)
			{
				_className = getQualifiedClassName(observableComponent).split("::").pop();
			}
			return _className;
		}

		/**
		 *
		 * @return
		 */
		public function get id():String
		{
			return _id;
		}

		/**
		 *
		 * @param id
		 */
		protected function parseStyleNames(id:String):void
		{
			defaultSelectorNames = id.split(ID_DELIMITER);
			_id = defaultSelectorNames.pop();

			// clean up selectors
			defaultSelectorNames.unshift("." + className);
			defaultSelectorNames.push("#" + this.id);
		}
	}
}

